package controller;

import dao.UsersDao;
import model.Users;
import util.SHA;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Hasee on 2018/11/28
 */
public class LoginController extends HttpServlet {
    public LoginController(){
        super();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest, HttpServletResponse)
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (req.getSession().getAttribute("users")!=null){
            //用户已经登录，不能再注册
            resp.sendRedirect(req.getContextPath()+"/index.jsp");
            return;
        }
        //设置编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String idusers = "";
        if (name == null||name.length()==0){
            req.setAttribute("error","用户名不能为空");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
            //resp.setContentType("text/html;charset=UTF-8");
            //resp.getWriter().append("用户名不能为空");
            return;
        }
        if (password == null || name.length() == 0) {
            req.setAttribute("error", "密码不能为空");
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
            //resp.setContentType("text/html;charset=UTF-8");
            //resp.getWriter().append("密码不能为空");
            return;
        }
        //使用SHA加密算法
        password = SHA.encrypt(password);
        Users user = new Users();
        user.setName(name);
        user.setPassword(password);
        UsersDao usersDao = new UsersDao();
        idusers = usersDao.userLogin(user);
        if (idusers.equals("false")){
            req.setAttribute("error","用户名或者密码错误");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
            return;
        }
        user.setIdusers(idusers);
        //登录成功，将用户信息设置到session中
        user.setPassword("");
        req.getSession().setAttribute("users",user);
        req.getSession().setAttribute("name",user.getName());
        req.getSession().setMaxInactiveInterval(14400);
        System.out.println(req.getSession().getAttribute("users"));
        //resp.sendRedirect(req.getContextPath()+"/index.jsp");
        resp.sendRedirect(req.getContextPath()+"/personalHomepage.jsp");
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest, HttpServletResponse)
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //重定向到登录页面
        req.getRequestDispatcher("/login.jsp").forward(req,resp);
    }
}
